package service

import (
	"flag"
	"fmt"
	"os"
	"strings"
)

var (
	serviceName     = flag.String("name", "", "服务名称 比如：kelvins-template")
	serviceAction   = flag.String("action", "list", "服务action 比如：list，clear，ping")
	serviceSequence = flag.String("sequence", "", "服务节点(端口号) 比如：50024")
)

func Touch() {
	if *serviceName == "" {
		fmt.Fprintln(os.Stderr, "name参数不应该为空")
		return
	}
	serviceNames := strings.Split(*serviceName, ",")
	serviceSequences := strings.Split(*serviceSequence, ",")
	switch *serviceAction {
	case "list":
		for _, name := range serviceNames {
			if name == "" {
				continue
			}
			listServiceNode(name)
		}
	case "clear":
		for _, sequence := range serviceSequences {
			if sequence == "" {
				fmt.Fprintln(os.Stderr, "清理服务，sequence参数不能为空")
				continue
			}
			clearServiceNode(*serviceName, sequence)
		}
	case "ping":
		for _, sequence := range serviceSequences {
			pingServiceNode(*serviceName, sequence)
		}
	default:
		fmt.Fprintln(os.Stderr, "不支持的action")
	}
}
